//
//  DDVerifyInfoManger.swift
//  DingDongZP
//
//  Created by ZZN on 17/1/2.
//  Copyright © 2017年 51ledian.com. All rights reserved.
//

import UIKit
import ReactiveCocoa
class DDVerifyInfoManger: NSObject {
    
    
    dynamic var myResumeDict:[String:AnyObject]?
    dynamic var jobfinderUserInfoDict:[String:AnyObject]?
    
    
    private var isAccClosure:((arrTup:[Bool])->())?
    private var isAccClosureBool:(Bool->())?
    
    
//    class var shareManger:DDVerifyInfoManger {
//        
//        struct Instance {
//            
//            static let instance = DDVerifyInfoManger()
//        }
//        return Instance.instance
//    }
    
    override init() {
        super.init()
//        handle()
        initKVO()
    }
    
    //
    func isAccClosue(closure:(arrRup:[Bool])->()) {
        
        isAccClosure = closure
    }
    
    func isAccClosureBool(closure:(Bool)->()) {
        
        isAccClosureBool = closure
    }
    
    //initkvo
    func initKVO() {
        
        let singal1 = rac_valuesForKeyPath("myResumeDict", observer: self).filter { (obj) -> Bool in
            
            if obj == nil {
                
                return false
            }
            return true
        }
        
        let singal2 = rac_valuesForKeyPath("jobfinderUserInfoDict", observer: self).filter { (obj) -> Bool in
            
            if obj == nil {
                
                return false
            }
            return true
        }
        
        RACSignal.zip([singal1,singal2]).subscribeNext { [weak self] (obj) in
            
            self?.personInfoHandle()
        }
    }
    //数据校验
    func personInfoHandle() {
        
        guard let resumeModel = DDMyResumeModel.yy_modelWithDictionary(myResumeDict!) else { return }
        guard let userModel = LDUserInfoModel.yy_modelWithDictionary(jobfinderUserInfoDict!) else { return }
        
        
        //根据UI布局分布校验
        //基本信息
        let singal1 = RACSubject()
        //期望工作
        let singal2 = RACSubject()
        //亮点优势
        let singal3 = RACSubject()
        //工作经历
        let singal4 = RACSubject()
        //教育经历
        let singal5 = RACSubject()
        
        
        
        
        RACSignal.combineLatest([singal1,singal2,singal3,singal4,singal5]).subscribeNext { [weak self] (obj) in
            
            guard let tuple = obj as? RACTuple else { return }
            guard let arrTup = tuple.allObjects() else { return }
            if self?.isAccClosure == nil { return }
            
            let newarr = arrTup.map({ (obj) -> Bool in
                
                return obj as? Bool ?? false
            })
            
            if self?.isAccClosure != nil {
                self?.isAccClosure!(arrTup: newarr)
            }
            
            
            
            if newarr.contains(false) &&  self?.isAccClosureBool != nil && self?.isAccClosureBool != nil{
                
                self?.isAccClosureBool!(true)
            } else if self?.isAccClosureBool != nil {
                self?.isAccClosureBool!(false)
            }
            
        }

        
        singal1.sendNext(isAccessBaseInfo(userModel))
        singal2.sendNext(isAccessExpjob(userModel))
        singal3.sendNext(isAccHeightlightAndAdvantage(resumeModel))
        singal4.sendNext(isAccJobExp(resumeModel))
        singal5.sendNext(isAccJobExp(resumeModel))
    }
    
    //简单true处理
    func getDataVerifyClosure(isAcc:(isAcc:Bool)->()) {
        
        
        handle()
        let singal1 = rac_valuesForKeyPath("myResumeDict", observer: self).filter { (obj) -> Bool in
            
            if obj == nil {
                
                return false
            }
            return true
        }
        
        let singal2 = rac_valuesForKeyPath("jobfinderUserInfoDict", observer: self).filter { (obj) -> Bool in
            
            if obj == nil {
                
                return false
            }
            return true
        }
        
        RACSignal.zip([singal1,singal2]).subscribeNext { [weak self] (obj) in
            
            
            //成功接收
            guard let resumeModel = DDMyResumeModel.yy_modelWithDictionary(self!.myResumeDict!) else { return }
            guard let userModel = LDUserInfoModel.yy_modelWithDictionary(self!.jobfinderUserInfoDict!) else { return }
            
            //根据UI布局分布校验
            //基本信息
            let singal1 = RACSubject()
            //期望工作
            let singal2 = RACSubject()
            //亮点优势
            let singal3 = RACSubject()
            //工作经历
            let singal4 = RACSubject()
            //教育经历
            let singal5 = RACSubject()
            
            
            
            
            RACSignal.combineLatest([singal1,singal2,singal3,singal4,singal5]).subscribeNext { (obj) in
                
                guard let tuple = obj as? RACTuple else { return }
                guard let arrTup = tuple.allObjects() else { return }
                
                let newarr = arrTup.map({ (obj) -> Bool in
                    
                    return obj as? Bool ?? false
                })
                
                
                
                
                if newarr.contains(false) {
                    
                    isAcc(isAcc: false)
                } else {
                    
                    isAcc(isAcc: true)
                }
            }
            
            singal1.sendNext(self!.isAccessBaseInfo(userModel))
            singal2.sendNext(self!.isAccessExpjob(userModel))
            singal3.sendNext(self!.isAccHeightlightAndAdvantage(resumeModel))
            singal4.sendNext(self!.isAccJobExp(resumeModel))
            singal5.sendNext(self!.isAccJobExp(resumeModel))
            
            
        }
    }
    
    
    //base info
    func isAccessBaseInfo(model:LDUserInfoModel) ->Bool {
        
        
        if model.name == nil { return false }
        if model.sex == nil  { return false }
        if model.experience == nil  { return false }
        if model.weixin == nil  { return false }
        
        
        //        debugPrint(name,sex,experience,weixin)
        return true
    }
    //expect job
    func isAccessExpjob(model:LDUserInfoModel) ->Bool  {
        
        guard model.expectedjobs?.count > 0 else { return false }
        return true
    }
    //heightlights advantange
    func isAccHeightlightAndAdvantage(model:DDMyResumeModel) ->Bool {
        
        
        let isEditedHigh:Bool = {
            
            if model.highlights.count > 0 || model.audiohighlights_isset == "1" {
                
                return true
            }
            return false
        }()
        
        let isEditedStreng:Bool = {
            
            if model.mystrengthstext != nil || model.audiomystrengths_isset == "1" {
                
                return true
            }
            return false
        }()
        
        
        if isEditedHigh && isEditedStreng {
            
            return true
        }
        
        return false
    }
    
    //jobexperience
    func isAccJobExp(model:DDMyResumeModel) ->Bool {
        
        if model.workexperience.count > 0 || model.audioworkexperience_isset == "1" {
            
            return true
        }
        return false
    }
    
    //eduexp
    func isAccEduExp(model:DDMyResumeModel) ->Bool {
        
        if model.eduexperience.count > 0  || model.audioeduexperience_isset == "1"  {
            
            return true
        }
        return false
    }
    
    //处理
    func handle() {
        
        
        if LDUserInfo.sharedInstance.userInfoDict.count == 0 {
            
            LDUserInfo.sharedInstance.queryUserInfo({ [weak self] (dict) in
                
                self?.jobfinderUserInfoDict = dict
                
                }, failure: nil)
        } else {
            
            jobfinderUserInfoDict = LDUserInfo.sharedInstance.userInfoDict
        }
        
        if LDUserInfo.sharedInstance.myRemuseInfoDict.count == 0 {
            
            LDUserInfo.sharedInstance.queryJobFinderMyResume({ [weak self] (dict) in
                
                self?.myResumeDict = dict
                }, failure: nil)
            
        } else {
            
            myResumeDict = LDUserInfo.sharedInstance.myRemuseInfoDict
        }
    }
}
